package com.longlong.controller.adminApi;


import com.longlong.config.globalExceptionClass.BusinessException;
import com.longlong.entity.AdminUser;
import com.longlong.service.impl.AdminUserServiceImpl;
import com.longlong.vo.LoginVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;

@RestController
@Api(value = "后台管理员接口")
public class admin_user {
    @Autowired
    private AdminUserServiceImpl adminUserService;

    @ApiOperation(value = "登楼接口")
    @PostMapping("/login")
    private Object login(@RequestBody LoginVo loginvo){
        return adminUserService.login(loginvo);
    }

    @ApiOperation(value = "获取管理员用户信息")
    @PostMapping("/getAdminUserInfo")
    public AdminUser getAdminUserInfo(Principal principal){
        if(principal == null){
            throw new BusinessException(401,"请登录!!!");
        }
        return adminUserService.findAdminUserInfoById();
    }

    @ApiOperation(value = "后台管理员推出登录")
    @PostMapping("/logout")
    public String logout(){
        //获取security信息
        SecurityContext context = SecurityContextHolder.getContext();
        if(context.getAuthentication() != null){
            context.setAuthentication(null);
        }
        return "退出登录成功";
    }
}
